package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.zretc.device.dao.DeviceDao;
import com.zretc.device.entity.Device;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DeviceModDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtdevCode;
	private JTextField txtdevName;
	private JTextField txtnowNum;
	private JTextField txthighNum;
	private JTextField txtlowNum;
	private JTextField txttotalNum;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DeviceModDialog dialog = new DeviceModDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public DeviceModDialog() {
		setBounds(100, 100, 615, 526);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("设备修改");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(203, 22, 182, 47);
		contentPanel.add(label);

		JLabel label_1 = new JLabel("设备编号");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(95, 82, 120, 30);
		contentPanel.add(label_1);

		txtdevCode = new JTextField();
		txtdevCode.setEditable(false);
		txtdevCode.setFont(new Font("宋体", Font.PLAIN, 25));
		txtdevCode.setColumns(10);
		txtdevCode.setBounds(229, 82, 250, 30);
		contentPanel.add(txtdevCode);

		JLabel label_2 = new JLabel("设备名称");
		label_2.setFont(new Font("宋体", Font.PLAIN, 25));
		label_2.setBounds(95, 128, 120, 30);
		contentPanel.add(label_2);

		txtdevName = new JTextField();
		txtdevName.setFont(new Font("宋体", Font.PLAIN, 25));
		txtdevName.setColumns(10);
		txtdevName.setBounds(229, 128, 250, 30);
		contentPanel.add(txtdevName);

		JLabel label_3 = new JLabel("现有库存");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(95, 171, 120, 30);
		contentPanel.add(label_3);

		txtnowNum = new JTextField();
		txtnowNum.setFont(new Font("宋体", Font.PLAIN, 25));
		txtnowNum.setColumns(10);
		txtnowNum.setBounds(229, 171, 250, 30);
		contentPanel.add(txtnowNum);

		JLabel label_4 = new JLabel("最大库存");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(95, 214, 120, 30);
		contentPanel.add(label_4);

		txthighNum = new JTextField();
		txthighNum.setFont(new Font("宋体", Font.PLAIN, 25));
		txthighNum.setColumns(10);
		txthighNum.setBounds(229, 214, 250, 30);
		contentPanel.add(txthighNum);

		JLabel label_5 = new JLabel("最小库存");
		label_5.setFont(new Font("宋体", Font.PLAIN, 25));
		label_5.setBounds(95, 257, 106, 30);
		contentPanel.add(label_5);

		txtlowNum = new JTextField();
		txtlowNum.setFont(new Font("宋体", Font.PLAIN, 25));
		txtlowNum.setColumns(10);
		txtlowNum.setBounds(229, 257, 250, 30);
		contentPanel.add(txtlowNum);

		JLabel label_6 = new JLabel("库存总数");
		label_6.setFont(new Font("宋体", Font.PLAIN, 25));
		label_6.setBounds(95, 300, 120, 30);
		contentPanel.add(label_6);

		txttotalNum = new JTextField();
		txttotalNum.setFont(new Font("宋体", Font.PLAIN, 25));
		txttotalNum.setColumns(10);
		txttotalNum.setBounds(229, 300, 250, 30);
		contentPanel.add(txttotalNum);

		JButton button = new JButton("返回");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(118, 360, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("修改");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(319, 360, 150, 38);
		contentPanel.add(button_1);
		// 窗口居中
		setLocationRelativeTo(null);
	}

	/**
	 * 带参数构造
	 * 
	 * @param device 参数是device对象
	 */
	public DeviceModDialog(Device device) {
		setBounds(100, 100,615, 526);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("设备修改");
		label.setFont(new Font("宋体", Font.PLAIN, 40));
		label.setBounds(203, 22, 182, 47);
		contentPanel.add(label);

		JLabel label_1 = new JLabel("设备编号");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(95, 82, 120, 30);
		contentPanel.add(label_1);

		txtdevCode = new JTextField();
		txtdevCode.setEditable(false);
		txtdevCode.setFont(new Font("宋体", Font.PLAIN, 25));
		txtdevCode.setColumns(10);
		txtdevCode.setBounds(229, 82, 250, 30);
		contentPanel.add(txtdevCode);

		JLabel label_2 = new JLabel("设备名称");
		label_2.setFont(new Font("宋体", Font.PLAIN, 25));
		label_2.setBounds(95, 128, 120, 30);
		contentPanel.add(label_2);

		txtdevName = new JTextField();
		txtdevName.setFont(new Font("宋体", Font.PLAIN, 25));
		txtdevName.setColumns(10);
		txtdevName.setBounds(229, 128, 250, 30);
		contentPanel.add(txtdevName);

		JLabel label_3 = new JLabel("现有库存");
		label_3.setFont(new Font("宋体", Font.PLAIN, 25));
		label_3.setBounds(95, 171, 120, 30);
		contentPanel.add(label_3);

		txtnowNum = new JTextField();
		txtnowNum.setFont(new Font("宋体", Font.PLAIN, 25));
		txtnowNum.setColumns(10);
		txtnowNum.setBounds(229, 171, 250, 30);
		contentPanel.add(txtnowNum);

		JLabel label_4 = new JLabel("最大库存");
		label_4.setFont(new Font("宋体", Font.PLAIN, 25));
		label_4.setBounds(95, 214, 120, 30);
		contentPanel.add(label_4);

		txthighNum = new JTextField();
		txthighNum.setFont(new Font("宋体", Font.PLAIN, 25));
		txthighNum.setColumns(10);
		txthighNum.setBounds(229, 214, 250, 30);
		contentPanel.add(txthighNum);

		JLabel label_5 = new JLabel("最小库存");
		label_5.setFont(new Font("宋体", Font.PLAIN, 25));
		label_5.setBounds(95, 257, 106, 30);
		contentPanel.add(label_5);

		txtlowNum = new JTextField();
		txtlowNum.setFont(new Font("宋体", Font.PLAIN, 25));
		txtlowNum.setColumns(10);
		txtlowNum.setBounds(229, 257, 250, 30);
		contentPanel.add(txtlowNum);

		JLabel label_6 = new JLabel("库存总数");
		label_6.setFont(new Font("宋体", Font.PLAIN, 25));
		label_6.setBounds(95, 300, 120, 30);
		contentPanel.add(label_6);

		txttotalNum = new JTextField();
		txttotalNum.setFont(new Font("宋体", Font.PLAIN, 25));
		txttotalNum.setColumns(10);
		txttotalNum.setBounds(229, 300, 250, 30);
		contentPanel.add(txttotalNum);

		if (device != null) {
			txtdevCode.setText(device.getDevice_code());
			txtdevName.setText(device.getDevice_name());
			txtnowNum.setText(Integer.toString(device.getNow_num()));
			txthighNum.setText(Integer.toString(device.getHigh_num()));
			txtlowNum.setText(Integer.toString(device.getLow_num()));
			txttotalNum.setText(Integer.toString(device.getTotal_num()));
		}

		JButton button = new JButton("返回");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 隐藏不可见
				setVisible(false);
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		button.setBounds(118, 360, 150, 38);
		contentPanel.add(button);

		JButton button_1 = new JButton("修改 ");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 获取文本框里的值
				// 设备id
				String device_code = txtdevCode.getText();
				// 设备名称
				String device_name = txtdevName.getText();
				// 现有库存,转型
				String tnow_num = txtnowNum.getText();
				// 最大库存
				String thigh_num = txthighNum.getText();
				// 最小库存
				String tlow_num = txtlowNum.getText();
				// 总数
				String ttotal_num = txttotalNum.getText();
				// 现有库存,转型
				int now_num = Integer.parseInt(tnow_num);
				// 最大库存
				int high_num = Integer.parseInt(thigh_num);
				// 最小库存
				int low_num = Integer.parseInt(tlow_num);
				// 总数
				int total_num = Integer.parseInt(ttotal_num);
				// 判断输入的值
				if (device_code.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "编号不能为空");
				} else if (device_name.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "名称不能为空");
				} else if (tnow_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "现有库存不能为空");
				} else if (thigh_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "最大库存不能为空");
				} else if (tlow_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "最低库存不能为空");
				} else if (ttotal_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "库存总数不能为空");
				} else {
					// 创建对象
					Device device = new Device(device_code, device_name, now_num, high_num, low_num, total_num);
					DeviceDao dd = new DeviceDao();
					boolean flag = dd.updateDev(device);
					if (flag) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "修改成功");
					} else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "修改失败");
					}
				}
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));
		button_1.setBounds(319, 360, 150, 38);
		contentPanel.add(button_1);

		// 窗口居中
		setLocationRelativeTo(null);
	}
}
